<template>
  <view class="page">
    <calendar v-if="priceType == 1" :date="localDate" :price-list="priceList" :show-half-price="true" :mode="'2'" :init-month-count="'1'" :switch-month="true">
      <text v-if="priceType == 1" style="font-size: 12px;color:#999;padding:0 10px;">说明:单日价格上面为全天价格,下面为半天价格</text>
    </calendar>
    <calendar v-if="priceType == 2" :date="localDate" :price-list="priceList" :mode="'2'" :init-month-count="'1'" :switch-month="true" />
  </view>
</template>
<script>
import calendar from '../components/calendar/calendar.vue'
import { getCarTypePriceList, getProductPriceList } from '@/common/api/car'
import { dateFormat, getBetweenDatePrice } from '@/common/util'
export default {
  components: {
    calendar
  },
  data() {
    return {
      priceList: [],
      carTypeDefaultPriceList: [], // 车型默认价格
      carTypeSpecialPriceList: [], // 车型特殊价格
      carTypeLists: [], // 车型列表
      carTypeId: '', // 车型的Id
      priceType: '', // 定价类型
      productDefaultPriceList: [], // 产品默认价格
      productSpecialPriceList: [], // 产品特殊价格
      productLists: [], // 产品列表
      productId: '', // 产品ID
      current: 1, // 当前分页
      localDate: '' // 当前日期
    }
  },
  onLoad(options) {
    this.localDate = dateFormat()
    this.priceType = options.priceType
    this.carTypeId = options.carTypeId
    if (options.priceType == 1) {
      this.carTypeList()
    } else if (options.priceType == 2) {
      this.productId = options.productId
      this.productList()
    }
  },
  methods: {
    // 车型定价列表获取默认价格
    async carTypeDefaultList() {
      const [err, res] = await getCarTypePriceList({
        vehicleTypeId: this.carTypeId,
        page: 1,
        defaultPrice: 0
      })
      if (!this.$http.errorCheck(err, res)) return false
      const carTypeInfo = res.data.data
      if (carTypeInfo.length > 0) {
        const start = this.localDate
        const dateArr = this.localDate.split('-')
        const end = (parseInt(dateArr[0]) + 1) + '-' + dateArr[1] + '-' + dateArr[2]
        this.carTypeDefaultPriceList = getBetweenDatePrice(start, end, carTypeInfo[0].price, carTypeInfo[0].halfPrice)
      }
      await this.carTypeList()
    },
    // 获取车型特殊定价列表
    async carTypeList() {
      const [err, res] = await getCarTypePriceList({
        vehicleTypeId: this.carTypeId,
        page: this.current,
        defaultPrice: 0
      })
      if (!this.$http.errorCheck(err, res)) return false
      const carTypeLists = res.data.data
      if (carTypeLists.length < 20) {
        this.carTypeLists = this.carTypeLists.concat(carTypeLists)
        this.creatPrice(this.carTypeLists)
      } else {
        this.carTypeLists = this.carTypeLists.concat(carTypeLists)
        this.current += 1
        await this.carTypeList()
      }
    },
    // 产品定价列表获取默认价格
    async productDefaultList() {
      const [err, res] = await getProductPriceList({
        productId: this.productId,
        vehicleTypeId: this.carTypeId,
        page: 1,
        defaultPrice: 0
      })
      if (!this.$http.errorCheck(err, res)) return false
      const productInfo = res.data.data
      if (productInfo.length > 0) {
        const start = this.localDate
        const dateArr = this.localDate.split('-')
        const end = (parseInt(dateArr[0]) + 1) + '-' + dateArr[1] + '-' + dateArr[2]
        this.productDefaultPriceList = getBetweenDatePrice(start, end, productInfo[0].price, '')
      }
      await this.productList()
    },
    // 获取产品特殊定价列表
    async productList() {
      const [err, res] = await getProductPriceList({
        productId: this.productId,
        vehicleTypeId: this.carTypeId,
        page: this.current,
        defaultPrice: 0
      })
      if (!this.$http.errorCheck(err, res)) return false
      const productLists = res.data.data
      if (productLists.length < 20) {
        this.productLists = this.productLists.concat(productLists)
        this.creatPrice(this.productLists)
      } else {
        this.productLists = this.productLists.concat(productLists)
        this.current += 1
        await this.productList()
      }
    },
    // 生成价格
    creatPrice(list) {
      for (let i = 0; i < list.length; i++) {
        if (this.priceType == 1) {
          const carTypeSpecialPriceList = getBetweenDatePrice(list[i].startDate, list[i].endDate, list[i].price, list[i].halfPrice)
          this.carTypeSpecialPriceList = this.carTypeSpecialPriceList.concat(carTypeSpecialPriceList)
        } else if (this.priceType == 2) {
          const productSpecialPriceList = getBetweenDatePrice(list[i].startDate, list[i].endDate, list[i].price, '')
          this.productSpecialPriceList = this.productSpecialPriceList.concat(productSpecialPriceList)
        }
      }
      // 补全默认价格
      if (this.priceType == 1) {
        this.$nextTick(() => {
          this.priceList = this.carTypeSpecialPriceList
        })
      } else if (this.priceType == 2) {
        this.$nextTick(() => {
          this.priceList = this.productSpecialPriceList
        })
      }
    }
  }
}
</script>

<style>

</style>
